package com.xinyue.game.center.business.role;

import com.xinyue.game.center.business.role.manager.PlayerCreateManager;
import com.xinyue.game.center.business.role.param.CreateRoleRequest;
import com.xinyue.game.center.business.role.param.CreateRoleResponse;
import com.xinyue.game.center.common.GameCenterConfig;
import com.xinyue.game.center.common.GameTokenService;
import com.xinyue.game.center.dao.entity.AccountEntity;
import com.xinyue.game.center.dao.entity.PlayerEntity;
import com.xinyue.game.center.dao.entity.ZonePlayerEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 王广帅
 * @since 2022/1/16 15:04
 */
@Service
public class PlayerService {

    private Logger logger = LoggerFactory.getLogger(PlayerService.class);
    @Autowired
    private PlayerCreateManager createRoleManager;
    @Autowired
    private GameCenterConfig centerConfig;
    @Autowired
    private GameTokenService gameTokenService;


    public CreateRoleResponse createPlayer(AccountEntity accountEntity, CreateRoleRequest createRoleParam) throws Exception {
        createRoleManager.checkNicknameSensitive(createRoleParam.getNickname());
        createRoleManager.checkRoleExist(accountEntity, createRoleParam.getZoneId());
        PlayerEntity roleEntity = createRoleManager.createRole(createRoleParam);
        ZonePlayerEntity zoneRoleEntity = createRoleManager.getZoneRole(roleEntity);
        accountEntity.getSectionPlayerMap().put(createRoleParam.getZoneId(), zoneRoleEntity);
        CreateRoleResponse createRoleResponse = new CreateRoleResponse();
        createRoleResponse.setPlayerId(zoneRoleEntity.getPlayerId());
        createRoleResponse.setNickname(roleEntity.getNickname());
        createRoleResponse.setCreateTime(roleEntity.getCreateTime());
        createRoleResponse.setLastLoginTime(roleEntity.getCreateTime());
        String token = gameTokenService.createPlayerToken(roleEntity.getUserId(), roleEntity.getPlayerId(), roleEntity.getZoneId());
        createRoleResponse.setToken(token);
        return createRoleResponse;
    }


}
